package com.gongjian.database.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.gongjian.database.entity.User;
import com.gongjian.utile.CodeEntity;
import com.gongjian.utile.Https;
import com.alibaba.fastjson.JSONArray;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.gongjian.utile.utile.Staic;

import com.gongjian.web.register.ResgisterInfo;
import org.apache.ibatis.session.SqlSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
 * @Author: TanLiLin
 * @Date: 2019/3/21 17:16
 * @Version 1.0
 */
import java.util.List;
@Service
public class WxUserService {

    @Autowired
    SqlSession sqlSession;


    /**
     * 微信小程序登录接口
     * @param code
     * @param session
     * @return
     */
    public String Wxlogin(String code,HttpSession session) {
        try {
            String re=Https.wxLoginApi(code);
            JSONObject jsonObject= JSON.parseObject(re);
            String openid=jsonObject.getString("openid");
            User where=new User();
            where.setUserWxid(openid);
            User user=sqlSession.selectOne("findOneUserByEverything",where);
            if(user==null){
                return new CodeEntity(5000,"未查询到你的微信，也许你为绑定微信！！！").toJSONString();
            }else {
                session.setAttribute("user",user);
                CodeEntity codeEntity=new CodeEntity(200,"OK");
                JSONObject data=new JSONObject();
                data.put("sessionId",session.getId());
                data.put("userName",user.getUserName());
                data.put("userNumber",user.getUserNumber());
                data.put("userType",user.getUserType());
                codeEntity.setData(data);
                return codeEntity.toJSONString();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new CodeEntity(5000,"服务器异常").toJSONString();
        }
    }

    /**
     * 绑定微信
     * @param code
     * @param userNumber
     * @param userName
     * @return
     */
    public  String bindWx(String code,String userNumber,String userName){
        try {
            String re=Https.wxLoginApi(code);
            JSONObject jsonObject= JSON.parseObject(re);
            String openid=jsonObject.getString("openid");
            User user=new User();
            user.setUserName(userName);
            user.setUserNumber(userNumber);
            User reUSer=sqlSession.selectOne("findUserByUserNumberAndUserName",user);
            if(reUSer==null){
                return  new CodeEntity(5000,"输入错误！！").toJSONString();
            }else{
                if(reUSer.getUserWxid()!=null&&reUSer.getUserWxid().length()>2){
                   return new CodeEntity(4009,"该学号已经绑定，不可更改。如需更改，请联系管理员！！").toJSONString();
                }
                User userM=new User();
                userM.setUserId(reUSer.getUserId());
                userM.setUserWxid(openid);
                int cout=sqlSession.update("updateUserUserWxidByUserId",userM);
                sqlSession.commit();
                return  new CodeEntity(200,"绑定成功！！").toJSONString();
            }

        } catch (IOException e) {
            e.printStackTrace();
            return new CodeEntity(5000,"服务器异常").toJSONString();
        }
    }
    /**
     *  后台扫码登录
     * @param code
     * @param session
     * @return
     */
    public String WxBackLogin(double latitude,double longitude,String code,HttpSession session) {
        String codeEntit=Wxlogin(code, session);
        ResgisterInfo resgisterInfo=new ResgisterInfo();
        resgisterInfo.setLatitude(latitude);
        resgisterInfo.setLongitude(longitude);
        session.setAttribute("resgisterInfo",resgisterInfo);
        return codeEntit;
    }



}
